package leetcode;

import java.util.Arrays;
import java.util.PriorityQueue;

public class LC2542 {

    public static long maxScore(int[] nums1, int[] nums2, int k) {

        int[][] nums3 = new int[nums1.length][2];
        for (int i = 0; i < nums1.length; i++) {
            nums3[i][0] = nums1[i];
            nums3[i][1] = nums2[i];
        }
        Arrays.sort(nums3,(a ,b) -> {
            return b[1] - a[1];
        });

        PriorityQueue<Integer> queue = new PriorityQueue<>();
        long sum1 = 0;
        int minNum = nums3[k-1][1];

        for (int i = 0; i < k; i++) {
            queue.offer(nums3[i][0]);
            sum1 += nums3[i][0];
        }
        long score  = sum1 * minNum;

        for (int i = k; i < nums1.length; i++) {
            int tmp = queue.peek();
            if (nums3[i][0] > tmp) {
                queue.poll();
                queue.offer(nums3[i][0]);
                sum1 += nums3[i][0] - tmp;
                score = Math.max(score, sum1 * nums3[i][1]);
            }
        }
        return score;

    }

    public static void main(String[] args) {
        maxScore(new int[]{1,3,2},new int[]{3,1,2},2);
    }
}
